package com.example.boot.utils;

import com.baomidou.mybatisplus.core.metadata.IPage;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ClassName：R
 * Description：
 *
 * @author yy
 * @date 2018/5/19 17:06
 **/
public class R<T> extends HashMap<String, Object> {
    private static final long serialVersionUID = 1L;

    public R() {
        put("code", 0);
        put("msg", "OK");
    }

    public static R error() {
        return error(500, "未知异常，请稍后");
    }

    public static R error(String msg) {
        return error(500, msg);
    }

    public static R error(int code, String msg) {
        R r = new R();
        r.put("code", code);
        r.put("msg", msg);
        return r;
    }

    public static R error(int code, String msg, List<?> data) {
        R r = new R();
        r.put("code", code);
        r.put("msg", msg);
        r.put("data", data);
        return r;
    }

    public static R ok(String msg) {
        R r = new R();
        r.put("msg", msg);
        return r;
    }

    public static R ok(int code, String msg) {
        R r = new R();
        r.put("code", code);
        r.put("msg", msg);
        return r;
    }

    public static R ok(int code, Object obj) {
        R r = new R();
        r.put("code", code);
        r.put("data", obj);
        return r;
    }


    public static R ok(Map<String, Object> map) {
        R r = new R();
        r.put("code", 0);
        r.put("data", map);
        return r;
    }

    public static R ok() {
        return new R();
    }

    @Override
    public R put(String key, Object value) {
        super.put(key, value);
        return this;
    }

    public static R ok(List<?> data, long total) {
        R r = new R();
        r.put("data", data);
        r.put("total", total);
        return r;
    }

    public static R ok(List<?> data) {
        R r = new R();
        r.put("data", data);
        r.put("code", 0);
        return r;
    }
    public static R ok(IPage<?> data) {

        R r = new R();
        r.put("data", data);
        r.put("code", 0);
        return r;
    }
    public static R ok(Object data) {

        R r = new R();
        r.put("data", data);
        r.put("code", 0);
        return r;
    }
    public static R ok(int code, List<?> data) {

        R r = new R();
        r.put("data", data);
        r.put("code", code);
        return r;
    }

    public static R ok(int code, String msg, List<?> data) {
        R r = new R();
        r.put("code", code);
        r.put("msg", msg);
        r.put("data", data);
        return r;
    }

}